import axios from 'axios';
import Qs from 'qs'
import { tansParams } from "@/utils/common";
import { Notification, MessageBox, Message, Loading } from 'element-ui'

// const projecturl = '152.136.221.124:18080/peril'
// const projecturl = '210.75.213.155:8081/peril';
// const projecturl = '192.168.1.157:8166';
const projecturl = '192.168.1.43:8166';
const baseurl = 'http://'+projecturl

// const baseurl = 'http://192.168.1.73:8091';
// const baseurl = 'http://localhost:8110';

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    baseURL: baseurl,
    timeout: 30000
});

service.interceptors.request.use(
    config => {
        const token = localStorage.getItem("Authorization")
        if (token) {
            // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers['Authorization'] = token // 请求头加上token
            // config.headers['Content-Type'] = 'application/json'
        }
        // get请求映射params参数
        if (config.method === 'get' && config.params) {
            let url = config.url + '?' + tansParams(config.params);
            url = url.slice(0, -1);

            config.params = {};
            config.url = url;
        }
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        // 未设置状态码则默认成功状态
        const code = response.data.code || 0;
        if (response.status === 200) {
            if (code == -1) {
                localStorage.removeItem('Authorization');
                location.href = '/dist';
            } else {
                return response.data;
            }
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);
function getaxiosPost(url, data) {
    return service.post(baseurl + url, Qs.stringify(data))
}

function getaxiosPost1(url, data) {
    return service.post(baseurl + url, data)
}

function getaxiosGet(url, data) {
    return service.get(baseurl + url, { params: data })
}
function dateFormat(time) {
    if (time === '' || time == null) {
        return ''
    } else {
        var date = new Date(time)
        var year = date.getFullYear()
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
        var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
        // var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
        // 拼接
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
        // return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
        // return year + '-' + month + '-' + day
    }
}
export default {
    service,
    getaxiosPost,
    getaxiosPost1,
    getaxiosGet,
    baseurl,
    dateFormat,
    projecturl
};
